#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pi=pair<int,int>;
#define fi first
#define se second
bool ST;
const int N=2e5+5,B=444,M=460;
int n,m,C,Id,fa[N],a[N],sz[N],dep[N],pre[N][15],rt[N],bl[N],ct[M][1<<15],ps[M],vis[N];
short to[M][N];
vector<int>e[N];
vector<pi>q[N];
void dfs(int x) {
    sz[x]=1,dep[x]=dep[fa[x]]+1;
    for(int y:e[x])memcpy(pre[y],pre[x],sizeof(pre[x])),pre[y][a[x]]=y,dfs(y),sz[x]+=sz[y];
    if(sz[x]>B)sz[x]=0,vis[x]=++Id,ps[Id]=x;
}
void dfs1(int x,int id,int v){ct[id][v]++;for(int y:e[x])dfs1(y,id,v|1<<a[y]);}
int find(int x,int S){int s=1;for(int y:e[x])if((S>>a[y]&1)&&!vis[y])s+=find(y,S);return s;}
bool ED;
int main() {
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    cerr<<"Memory: "<<(&ST-&ED)/1024.0/1024.0<<" MB\n";
    cin>>n>>m>>C;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=2;i<=n;i++)cin>>fa[i],e[fa[i]].push_back(i);
    dfs(1);
    for(int i=1;i<=n;i++)if(vis[i]) {
        int x=vis[i];dfs1(i,x,1<<a[i]);
        for(int j=0;j<C;j++)for(int k=0;k<1<<C;k++)if(k>>j&1)ct[x][k]+=ct[x][k^(1<<j)];
        for(int j=fa[i],k=1<<a[i];j;j=fa[j]) {
            k|=1<<a[j],to[x][j]=k;
            if(vis[j])break;
        }
    }
    while(m--) {
        int x,S,y=1;cin>>S>>x;
        if(S>>a[x]&1^1){cout<<"1\n";continue;}
        for(int i=0;i<C;i++)if((S>>i&1^1)&&dep[pre[x][i]]>dep[y])y=pre[x][i];
        int as=find(x=y,S);
        for(int i=1;i<=Id;i++)if(to[i][x]&&(S|to[i][x])==S)as+=ct[i][S];
        cout<<as<<'\n';
    }
    return 0;
}